package com.example.strategy;

import com.example.strategy.pay.AliPay;
import com.example.strategy.pay.Wxpay;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Description : TODO
 * @Author : ZYX
 * @Date : 2022/6/13 14:30
 * @Version : 1.0
 **/
@Component
public class SignGenFactory {

    @Autowired
    private SignGeneratorProperties signGeneratorProperties;

    @Autowired
    private PaySignProperties paySignProperties;

    @Autowired
    private AliPay aliPay;

    @Autowired
    private Wxpay wxpay;

    Field[] fields = this.getClass().getDeclaredFields();

    public GeneratorSignBs createGeneratorSignBs(String serviceCode) throws Exception{

        Object className = signGeneratorProperties.findGeneratorClassName(serviceCode);
        for (int i = 0; i < fields.length; i++) {
            // 获取配置属性
            String type = fields[i].getGenericType().toString().substring(6);
            if (type.equalsIgnoreCase(className.toString())){
                String getter = "get" + StringUtils.capitalize(fields[i].getName());
                Method method = this.getClass().getMethod(getter);
                GeneratorSignBs generatorSignBs = (GeneratorSignBs) method.invoke(this);
                generatorSignBs.setPaySignProperties(paySignProperties);
                return generatorSignBs;
            }
        }
        return null;

    }

    public AliPay getAliPay() {
        return aliPay;
    }

    public void setAliPay(AliPay aliPay) {
        this.aliPay = aliPay;
    }

    public Wxpay getWxpay() {
        return wxpay;
    }

    public void setWxpay(Wxpay wxpay) {
        this.wxpay = wxpay;
    }
}
